<?php 
Class userController extends appController
{
	
	public function index()
	{
		
		$this->admin_priv('userindex');
	   $page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$user_list=Model('user')->adminList($page);
		$this->assign('ur_here','>管理员管理');
		$this->assign('search_action','index.php?g=admin&m=user&a=search');
		$this->assign('user_list',$user_list['data']);
		$this->assign('user_list_page',$user_list['pagelink']);
		$this->display('index.html');
	
	}
	
	public function role()
	{
		//$this->admin_priv('roleindex');
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$user_list=Model('user')->roleList($page);
		$this->assign('ur_here','>角色管理');
		$this->assign('user_list',$user_list['data']);
		$this->assign('user_list_page',$user_list['pagelink']);
		$this->display('role_list.html');
	
	}
	public function add_role()
	{
		$this->admin_priv('addrole');
		$action_list=Model('user')->get_admin_action();
		$this->assign('action','new_role');
		$this->assign('action_list',$action_list);
		$this->display('role.html');
	
	}
	public function excel()
	{
		@ini_set("max_execution_time", "0");
		@ini_set('memory_limit',          '10M');
		$file=current($_FILES);
		$result=array('error'=>1,'url'=>'');
		$thumb_url=Model('image')->upload_image($file, 'excel/'.date('Ym'),'','|XLS|');
		//$thumb_url='/vip.xls';
		if($thumb_url)
		{
			$result['error']=0;
		   $result['url']=urlencode($thumb_url);
		
		}
		print_r(json_encode($result));
		exit();
	
		
		
	}
	public function import_user()
	{
		$thumb_url=urldecode($_GET['f']);
		if($thumb_url)
		{
			header("Content-type: text/html; charset=utf-8");
			echo "/********Begin*********/<br>";
		   $res=Model('user')->importData($thumb_url);
		   if($res)
		  {
			//@unlink(ROOT_PATH.$thumb_url);
			echo "/********Success*********/<br>";
		 	//$this->showMsg(Lang('SUCCESS_ADD'));
		  }
		  else
		  {
		  	echo "/********Failer*********/<br>";
			//$this->showMsg(Lang('INVALID_OPERATION'));
		   }
		}
		
	}
	public function user_list()
	{
		//$this->admin_priv('userlist');
		$sort['keyword']=isset($_GET['k'])?trim($_GET['k']):'';
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$user_list=Model('user')->userList($page,$sort);
		$this->assign('ur_here','>用户管理');
		$this->assign('search_action','index.php?g=admin&m=user&a=search_user');
		$this->assign('user_list',$user_list['data']);
		$this->assign('user_list_page',$user_list['pagelink']);
		$this->display('user_list.html');
	
	}
	public function search_user()
	{
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$keyword=isset($_GET['k'])?trim($_GET['k']):'';
		$user_list=Model('user')->search_user($page,$keyword);
		$this->assign('ur_here','>用户搜索');
		$this->assign('search_action','index.php?g=admin&m=user&a=search_user');
	
		$this->assign('user_list',$user_list['data']);
		$this->assign('user_list_page',$user_list['pagelink']);
		$this->display('user_list.html');
	}
	public function add_user()
	{
	
		$this->admin_priv('createuser');
		$this->assign('action','create_user');
		$this->display('user.html');
	}
	public function create_user()
	{
		
		$info['phone']=isset($_POST['phone'])?trim($_POST['phone']):'';
		$info['realname']=isset($_POST['realname'])?($_POST['realname']):'';
		$info['area']=isset($_POST['area'])?$_POST['area']:'';
		$info['password']=isset($_POST['new_password'])?trim($_POST['new_password']):'';
		$confirm_password=isset($_POST['confirm_password'])?trim($_POST['confirm_password']):'';
		$user=Model('user');
		if($user->is_repeat($info['phone'],'phone'))
		{
			$this->showMsg(Lang('电话号码已被注册'));
		}
		if( $info['password']!=''&&$confirm_password!='')
		{
			if($confirm_password!=$info['password'])
			{
				$this->showMsg(Lang('twice_pwd_not_match'));
			}
			if( strlen($info['password'])<4)
			{
				$this->showMsg(Lang('PWD_TOO_SHORT'));
			}
		}
		else
		{
			$this->showMsg(Lang('PWD_TOO_SHORT'));
		}
		if(isset($info['password']))
		{
			$info['password']=md5($info['password']);
		}
	
		if(Model('user')->insert($info,'users'))
		{
			$this->showMsg(Lang('SUCCESS_ADD'));
		}
		else
		{
			$this->showMsg(Lang('INVALID_OPERATION'));
		}
	
	}
	public function edit_user()
	{
		
		$this->admin_priv('edituser');
		$id=$_GET['id']?intval($_GET['id']):0;
		$user_info=Model('user')->getUserInfo($id);
		$this->assign('user_info',$user_info);
		$this->assign('action','update_user');
		$this->display('user.html');
	}
	public function del_user()
	{
		$this->admin_priv('deluser',2);
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:0;
		$result=array('error'=>0,'content'=>'');
		$res=Model('user')->del(array('id'=>$id,'table'=>'users'));
		
		print_r(json_encode($result));
	}
	public function update_user()
	{
		$id=$_POST['id']?intval($_POST['id']):0;
		$info['phone']=isset($_POST['phone'])?trim($_POST['phone']):'';
		$info['realname']=isset($_POST['realname'])?($_POST['realname']):'';
		$info['area']=isset($_POST['area'])?$_POST['area']:'';
		$info['password']=isset($_POST['new_password'])?trim($_POST['new_password']):'';
		$confirm_password=isset($_POST['confirm_password'])?trim($_POST['confirm_password']):'';
	
		if( $info['password']!=''&&$confirm_password!='')
		{
			if($confirm_password!=$info['password'])
			{
				$this->showMsg(Lang('twice_pwd_not_match'));
			}
			if( strlen($info['password'])<4)
			{
				$this->showMsg(Lang('PWD_TOO_SHORT'));
			}
		}
		else
		{
			unset($info['password']);
		}
			if(isset($info['password']))
			{
				$info['password']=md5($info['password']);
			}
	
			if(Model('user')->update($info,array('id'=>$id,'table'=>'users')))
			{
				$this->showMsg(Lang('SUCCESS_EDIT'));
			}
			else
			{
				$this->showMsg(Lang('INVALID_OPERATION'));
			}
		
	}
	public function edit_admin()
	{
		$this->admin_priv('editadmin');
		$id=$_GET['id']?intval($_GET['id']):0;
		$user_info=Model('user')->getAdminInfo($id);
		
		$this->assign('user_info',$user_info);
		$this->assign('ur_here','>编辑用户');
		$user_group=Model('user')->get_role_list();
		$this->assign('id',$id);
	
		$this->assign('action','update_admin');
		$this->assign('user_group',$user_group);
		
		
		$this->display('add_user.html');
	}
	public function edit_role()
	{
		$this->admin_priv('editrole');
		$id=$_GET['id']?intval($_GET['id']):0;
		$action_list=Model('user')->get_admin_action();
		
		$user_info=Model('user')->getRoleInfo($id);
		if($user_info)
		{
			$user_action=explode(',',$user_info['role_action']);
			foreach($action_list as $k=>$v)
			{
				if(in_array($v['id'],$user_action))
				{
					$action_list[$k]['selected']=1;
				}
				else 
				{
					$action_list[$k]['selected']=0;
				}
			}
		}
		$this->assign('action_list',$action_list);
	
		$this->assign('user_info',$user_info);
		$this->assign('ur_here','>编辑角色');
	
		$this->assign('id',$id);
		$this->assign('action','update_role');
		$this->assign('user_group',$user_group);
	
	
		$this->display('role.html');
	}
	public function new_role()
	{
		
		$info['role_name']=isset($_POST['rolename'])?trim($_POST['rolename']):'';
		$info['role_des']=isset($_POST['roledes'])?trim(($_POST['roledes'])):'';
		$info['role_action']=isset($_POST['action'])?implode(',',$_POST['action']):'';

		if(Model('user')->roleEisted($info['role_name']))
		{
			if(Model('user')->insertRole($info))
			{
				$this->showMsg(Lang('SUCCESS_ADD'));
			}
			else
			{
				$this->showMsg(Lang('INVALID_OPERATION'));
			}
		}
		else 
		{
			$this->showMsg(Lang('ROLE_EXISTED'));
		}
		
	}
	public function update_role()
	{
		$id=$_POST['id']?intval($_POST['id']):0;
		$info['role_name']=isset($_POST['rolename'])?trim($_POST['rolename']):'';
		$old_name=isset($_POST['old_name'])?trim($_POST['old_name']):'';
		$info['role_des']=isset($_POST['roledes'])?trim(($_POST['roledes'])):'';
		$info['role_action']=isset($_POST['action'])?implode(',',$_POST['action']):'';
	
		if($old_name!=$info['role_name']&&!Model('user')->roleEisted($info['role_name']))
		{
			$this->showMsg(Lang('ROLE_EXISTED'));
			
		}
		else
		{
			if(Model('user')->updateRole($info,$id))
			{
				$this->showMsg(Lang('SUCCESS_EDIT'));
			}
			else
			{
				$this->showMsg(Lang('INVALID_OPERATION'));
			}
		}
	
	}
	public function del_role()
	{
		$this->admin_priv('delrole',2);
		$id=isset($_GET['id'])?intval($_GET['id']):0;
	
		$result=Model('user')->del_role($id);
		
		print_r(json_encode($result));
	}
	public function update_admin()
	{
		$id=$_POST['id']?intval($_POST['id']):0;
		$info['username']=isset($_POST['username'])?trim($_POST['username']):'';
		$info['qq']=isset($_POST['qq'])?intval(($_POST['qq'])):'';
		$info['email']=isset($_POST['email'])?$_POST['email']:'';
		$info['group_id']=isset($_POST['group_id'])?intval($_POST['group_id']):'';
		$info['password']=isset($_POST['new_password'])?trim($_POST['new_password']):'';
		$confirm_password=isset($_POST['confirm_password'])?trim($_POST['confirm_password']):'';
		
		if( $info['password']!=''&&$confirm_password!='')
		{
			if($confirm_password!=$info['password'])
			{
				$this->showMsg(Lang('twice_pwd_not_match'));
			}
			if( strlen($info['password'])<6)
			{
				$this->showMsg(Lang('PWD_TOO_SHORT'));
			}
		}
		else 
		{
			unset($info['password']);
		}
		
		if (!preg_match("/^[a-z]([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/i ",$info['email']))
		{
			$this->showMsg(Lang('EMAIL_FORMAT_ERROR'));
			 
		}
		 
		
		
		if( Model('user')->checkInfo($info['username'],$info['email'],$id))
		{
			if(Model('user')->updateInfo($info,$id))
			{
				$this->showMsg(Lang('SUCCESS_EDIT'));
			}
			else
			{
				$this->showMsg(Lang('INVALID_OPERATION'));
			}
		}
		else
		{
			$this->showMsg(Lang('INVALID_OPERATION'));
		}
	}
	public function add_admin()
	{
		$this->admin_priv('addadmin');
		$user_group=Model('user')->get_role_list();
		$this->assign('ur_here','>添加用户');
		$this->assign('action','insert_user');
		$this->assign('user_group',$user_group);
		$this->display('add_user.html');
	}

	public function del_admin()
	{
		$this->admin_priv('deluser',2);
		$id=isset($_GET['id'])?intval($_GET['id']):0;
		
		$result=Model('user')->del_user($id);
		if($result['error']==0)
		{
		   if($_SESSION['admin_id']==$id)
		  {
		       $this->logout(1);
		   }
		}
		print_r(json_encode($result));
	}
	public function insert_user()
	{
		
		
		$info['username']=isset($_POST['username'])?trim($_POST['username']):'';
	    $info['qq']=isset($_POST['qq'])?intval(($_POST['qq'])):'';
	    $info['email']=isset($_POST['email'])?$_POST['email']:'';
	    $info['group_id']=isset($_POST['group_id'])?intval($_POST['group_id']):'';
	    $info['password']=isset($_POST['new_password'])?trim($_POST['new_password']):'';
	    $confirm_password=isset($_POST['confirm_password'])?trim($_POST['confirm_password']):'';
	   
	    if( $info['password']!=''&&$confirm_password!='')
	    {
	    	if($confirm_password!=$info['password'])
	    	{
	    		$this->showMsg(Lang('twice_pwd_not_match'));
	    	}
	    }
	    if( strlen($info['password'])<6)
	    {
	    	$this->showMsg(Lang('pwd_too_short'));
	    }
	    if (!preg_match("/^[a-z]([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/i ",$info['email']))
	    {
	    	$this->showMsg(Lang('email_format_error'));
	      
	    }
	    
	   $result=Model('user')->isExist($info);
	   
	   if($result['error']>0)
	   {
	   	  $this->showMsg($result['content']);
	   }
	   else 
	   {
	   	   if(Model('user')->insertInfo($info))
	       {
	   	   	$this->showMsg(Lang('SUCCESS_ADD'));
	   	   }
	   	   else 
	   	   {
	   	   	$this->showMsg(Lang('INVALID_OPERATION'));
	   	   }
	   }
	    
	    
	}
	
	public function logout($ajax=0)
	{
	
	
		cookie('QIAQIA[admin_id]',   '');
		cookie('QIAQIA[admin_pass]', '');
		
		
		
		session('[destroy]');
		if($ajax==0)
		{
			header('Location:./ws/qq_admin.php');
		}
	   
	
	}
	public function userinfo()
	{
	
		$userInfo=Model('user')->getAdminInfo();
		
		$this->assign('ur_here','>用户资料');
		$this->assign('user_info',$userInfo);
		$this->display('userinfo.html');
	
	}
	
	public function update_info()
	{
		$info['username']=isset($_POST['username'])?trim($_POST['username']):'';
	    $info['qq']=isset($_POST['qq'])?intval(($_POST['qq'])):'';
	    $info['email']=isset($_POST['email'])?$_POST['email']:'';
	    $info['password']=isset($_POST['new_password'])?trim($_POST['new_password']):'';
	    $confirm_password=isset($_POST['confirm_password'])?trim($_POST['confirm_password']):'';
		
	    if( $info['password']!=''&&$confirm_password!='')
	    {
	       if($confirm_password!=$info['password'])
	      {
	    	$this->showMsg(Lang('TWICE_PWD_NOT_MATCH'));
	      }
	    }
	    
	   if( Model('user')->checkInfo($info['username'],$info['email']))
	   {
	   	$result=Model('user')->saveInfo($info);
	   	   
	   	    if($result['error']<0)
	   	    {
	   	     	$this->showMsg(Lang('SUCCESS_EDIT'),'index.php?g=admin&m=index&a=login');
	   	    }
	   	    elseif($result['error']==0)
	   	   {
	   	   	 $this->showMsg(Lang('SUCCESS_EDIT'),'index.php?g=admin&m=user&a=userinfo');
	   	   }
	   	   else 
	   	   {
	   	   	$this->showMsg(Lang('INVALID_OPERATION'),'index.php?g=admin&m=user&a=userinfo');
	   	   }
	   }
	    
	}
	
	
}